import collections


class Solution(object):
    def treeDiameter(self, edges):
        """
        :type edges: List[List[int]]
        :rtype: int
        """
        n = len(edges) + 1
        d = collections.defaultdict(set)
        self.max = 0
        for edge in edges:
            d[edge[0]].add(edge[1])
            d[edge[1]].add(edge[0])
        already_set = set()

        def search(id):
            already_set.add(id)
            count = []
            for next_id in d[id]:
                if next_id not in already_set:
                    count.append(search(next_id))
            if not count:
                return 1
            if len(count) == 1:
                self.max = max(self.max, count[0])
                return count[0] + 1
            else:
                count.sort(reverse=True)
                self.max = max(self.max, count[0] + count[1])
                return count[0] + 1
        search(0)
        return self.max


data = Solution()
edges = [[0, 1], [1, 2], [2, 3], [1, 4], [4, 5]]
print(data.treeDiameter(edges))
